package edu.guigu.networkdemo;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import edu.guigu.networkdemo.databinding.ActivityRetrofitDemoBinding;
import edu.guigu.networkdemo.model.User;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Path;

public class RetrofitDemoActivity extends AppCompatActivity {

    private ActivityRetrofitDemoBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);

//        setContentView(R.layout.activity_retrofit_demo);
        binding = ActivityRetrofitDemoBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        ViewCompat.setOnApplyWindowInsetsListener(binding.main, (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initData();
    }

    private void initData() {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .build();

        // 1 创建Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl("https://api.github.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        // 2 生成网络请求接口实例 (API -> JAVA接口)
        DemoApi demoApi = retrofit.create(DemoApi.class);
        // 3 使用(一般在单击后来调用)
        Call<User> call = demoApi.getUser("34");
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                // 一定要加了GSON转换器，retrofit才有帮我们解析出user
                User user = response.body();
                // UI更新
                runOnUiThread(() -> {
                    binding.itemUser.tvName.setText(user.getName());
                    binding.itemUser.tvCreatedAt.setText(user.getCreated_at());
                    binding.itemUser.tvUpdatedAt.setText(user.getUpdated_at());
                });
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {

            }
        });
    }

    interface DemoApi {
        @GET("users/{aaa}")
        Call<User> getUser(@Path("aaa") String index);

    }
}